<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Vue3.0初探 |  Shimmer
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/myBlog/images/ayer.svg" />
  
  
<link rel="stylesheet" href="/myBlog/dist/main.css">

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">
  
<link rel="stylesheet" href="/myBlog/css/custom.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

<link rel="alternate" href="/myBlog/atom.xml" title="Shimmer" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-2021-02-26-vue3笔记"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Vue3.0初探
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/myBlog/2021/02/26/2021-02-26-vue3%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2021-02-25T16:00:00.000Z" itemprop="datePublished">2021-02-26</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">4.1k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">18 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="✨-官网地址"><a href="#✨-官网地址" class="headerlink" title="✨ 官网地址"></a>✨ 官网地址</h2><p>官网文档：<a target="_blank" rel="noopener" href="https://v3.cn.vuejs.org/">https://v3.cn.vuejs.org/</a></p>
<h2 id="一、vue3安装与项目创建【掌握】"><a href="#一、vue3安装与项目创建【掌握】" class="headerlink" title="一、vue3安装与项目创建【掌握】"></a>一、vue3安装与项目创建【掌握】</h2><p>参考地址：<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/installation.html">https://www.vue3js.cn/docs/zh/guide/installation.html</a></p>
<h3 id="准备工作：升级脚手架"><a href="#准备工作：升级脚手架" class="headerlink" title="准备工作：升级脚手架"></a>准备工作：升级脚手架</h3><p>对于 Vue 3，你应该使用 <code>npm</code> 上可用的 Vue CLI v4.5 作为 <code>@vue/cli@next</code>。要升级，你应该需要全局重新安装最新版本的 <code>@vue/cli</code>：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">yarn global add @vue/cli@next</span><br><span class="line"><span class="meta">#</span><span class="bash"> OR</span></span><br><span class="line">npm install -g @vue/cli@next</span><br></pre></td></tr></table></figure>
<p>然后在 Vue 项目运行：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vue upgrade --next</span><br></pre></td></tr></table></figure>
<h3 id="方式1：以原有vueCli脚手架创建"><a href="#方式1：以原有vueCli脚手架创建" class="headerlink" title="方式1：以原有vueCli脚手架创建"></a>方式1：以原有vueCli脚手架创建</h3><p>创建项目：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vue create demo</span><br></pre></td></tr></table></figure>
<p>直接选择 <code>Vue 3 Preview</code> ，并回车，不考虑eslint问题。</p>
<img src="https://i.loli.net/2021/02/03/cxOksAVuJhP1eLN.png" style="border: 2px solid darkred" />

<p>然后执行：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 进入目录</span></span><br><span class="line"><span class="built_in">cd</span> demo</span><br><span class="line"><span class="comment"># 用vscode打开当前目录</span></span><br><span class="line">code .</span><br><span class="line"><span class="comment"># 运行项目</span></span><br><span class="line">yarn serve</span><br></pre></td></tr></table></figure>
<h3 id="方式2：vite创建"><a href="#方式2：vite创建" class="headerlink" title="方式2：vite创建"></a>方式2：vite创建</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">npm init vite-app demo</span><br><span class="line"><span class="meta">#</span><span class="bash"> 相当于</span></span><br><span class="line">npx create-vite-app demo</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 安装后执行</span></span><br><span class="line">npm install</span><br><span class="line"><span class="meta">#</span><span class="bash"> or</span></span><br><span class="line">yarn</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 运行项目</span></span><br><span class="line">npm run dev</span><br><span class="line"><span class="meta">#</span><span class="bash"> or</span></span><br><span class="line">yarn dev</span><br></pre></td></tr></table></figure>
<p>创建项目并运行，会发现我们 <code>npm run dev</code> 后是秒开项目的，运行速度极快。</p>
<h2 id="二、Composition-API（组合式API）【掌握】"><a href="#二、Composition-API（组合式API）【掌握】" class="headerlink" title="二、Composition API（组合式API）【掌握】"></a>二、Composition API（组合式API）【掌握】</h2><blockquote>
<p>相当于 React Hooks</p>
</blockquote>
<p>我们先使用以前vue2的方式实现一个累加：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h2&gt;&#123;&#123;count&#125;&#125;&lt;&#x2F;h2&gt;</span><br><span class="line">  &lt;button @click&#x3D;&quot;btnClick&quot;&gt;累加&lt;&#x2F;button&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">    data()&#123;</span><br><span class="line">        return &#123;</span><br><span class="line">            count: 0</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    methods: &#123;</span><br><span class="line">        btnClick()&#123;</span><br><span class="line">            this.count++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<p>这套代码可以实现一个累加的效果，但如果以后我们想把这个组件中的 <code>count</code> 字段与 <code>btnClick</code> 单独拎出来管理，那就比较麻烦了，因为 <code>count</code> 和 <code>btnClick</code> 不在同一个方法内，很难抽离。</p>
<h3 id="1、setup"><a href="#1、setup" class="headerlink" title="1、setup"></a>1、setup</h3><p>setup有以下特性：</p>
<blockquote>
<p>1、setup函数是处于 生命周期函数 beforeCreate 和 Created 两个钩子函数之间的函数 也就说在 setup函数中是无法 使用 data 和 methods 中的数据和方法的</p>
<p>2、setup函数是 Composition API（组合API）的入口</p>
<p>3、在setup函数中定义的变量和方法最后都是需要 return 出去的 不然无法再模板中使用</p>
<p>4、由于我们不能在 setup函数中使用 data 和 methods，所以Vue 为了避免我们错误的使用，直接将 setup函数中的this修改成了 undefined</p>
<p>5、setup函数只能是同步的不能是异步的</p>
</blockquote>
<h3 id="2、API-ref"><a href="#2、API-ref" class="headerlink" title="2、API - ref"></a>2、API - ref</h3><p>我们先来体验一下vue3怎么处理这个累加效果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h2&gt;&#123;&#123;count&#125;&#125;&lt;&#x2F;h2&gt;</span><br><span class="line">  &lt;button @click&#x3D;&quot;btnClick&quot;&gt;累加&lt;&#x2F;button&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import &#123;ref&#125; from &#39;vue&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">    data()&#123;</span><br><span class="line">        return &#123;</span><br><span class="line">            count: 0</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    setup()&#123;</span><br><span class="line">        const count &#x3D; ref(1);	&#x2F;&#x2F; 此时我们使用ref指定count的默认值为1，因此上面data中的count会失效</span><br><span class="line">        let btnClick &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">            count.value++;		&#x2F;&#x2F; 修改ref中的值要用xxx.value</span><br><span class="line">        &#125;</span><br><span class="line">        return &#123;count, btnClick&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时如果我想单独管理这个累加效果，我就可以这么操作：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h2&gt;&#123;&#123;count&#125;&#125;&lt;/h2&gt;</span><br><span class="line">  &lt;button @click=<span class="string">&quot;btnClick&quot;</span>&gt;累加&lt;/button&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">import</span> &#123;ref&#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="comment">// count: 0		// 一旦把setup中的代码抽离，return中对应的值要去掉，否则ref无效</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      	<span class="comment">// 函数调用后就会返回一个对象，因此我们直接return</span></span><br><span class="line">        <span class="keyword">return</span> clickCountFn()</span><br><span class="line">      	<span class="comment">// 如果后期还想同时返回其他数据，可以将clickCountFn()的返回结果展开</span></span><br><span class="line">      	<span class="comment">// return &#123;...clickCountFn(), 其他数据&#125;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 封装一个函数，这样这块功能我们就能单独管理了</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">clickCountFn</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">const</span> count = ref(<span class="number">1</span>);</span><br><span class="line">    <span class="keyword">let</span> btnClick = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        count.value++;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123;count, btnClick&#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h3 id="3、API-reactive"><a href="#3、API-reactive" class="headerlink" title="3、API - reactive"></a>3、API - reactive</h3><p>再来了解另一个API ：</p>
<blockquote>
<p>reactive函数和ref作用非常接近，但是它的参数是一个对象，我们可以在对象中定义其方法，而通过这个形式，就不需要再对其进行进行 <code>.value</code> 调用了。</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h2&gt;&#123;&#123;count&#125;&#125;&lt;&#x2F;h2&gt;</span><br><span class="line">  &lt;button @click&#x3D;&quot;btnClick&quot;&gt;累加&lt;&#x2F;button&gt;</span><br><span class="line">  &lt;p&gt;姓名：&#123;&#123;obj.username&#125;&#125;&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;button @click&#x3D;&quot;btnClick1&quot;&gt;修改姓名&lt;&#x2F;button&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import &#123;ref, reactive&#125; from &#39;vue&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">    setup()&#123;</span><br><span class="line">      	&#x2F;&#x2F; 使用reactive</span><br><span class="line">        let obj &#x3D; reactive(&#123;</span><br><span class="line">            username: &quot;Jack&quot;</span><br><span class="line">        &#125;)</span><br><span class="line">        let btnClick1 &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">            obj.username &#x3D; &quot;Mary&quot;</span><br><span class="line">        &#125;</span><br><span class="line">				return &#123;...clickCountFn(), obj, btnClick1&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function clickCountFn()&#123;</span><br><span class="line">    const count &#x3D; ref(1);</span><br><span class="line">    let btnClick &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">        count.value++;</span><br><span class="line">    &#125;</span><br><span class="line">    return &#123;count, btnClick&#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<p>使用 <code>reactive</code> 生成的对象与 <code>ref</code> 生成的值都是响应式的。</p>
<p>这里可以看到我们在 html 中调用数据时，使用的是 <code>obj.username</code> ，那我们是否可以直接写 <code>username</code> 呢？答案是可以的，但这里需要注意：</p>
<blockquote>
<p>由于reactive返回的对象本质上已经是一个Proxy对象，所以通过…扩展符号展开的属性，是无法进行响应式的</p>
</blockquote>
<p>也就是说，如果这么写：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> &#123;...clickCountFn(), ...obj, btnClick1&#125;</span><br></pre></td></tr></table></figure>
<p>那么是无法实现的。</p>
<h3 id="4、API-toRefs"><a href="#4、API-toRefs" class="headerlink" title="4、API - toRefs"></a>4、API - toRefs</h3><p>正确的写法应该是：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h2&gt;&#123;&#123;count&#125;&#125;&lt;&#x2F;h2&gt;</span><br><span class="line">  &lt;button @click&#x3D;&quot;btnClick&quot;&gt;累加&lt;&#x2F;button&gt;</span><br><span class="line">	&lt;!-- 无需obj.username，直接username即可 --&gt;</span><br><span class="line">  &lt;p&gt;姓名：&#123;&#123;username&#125;&#125;&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;button @click&#x3D;&quot;btnClick1&quot;&gt;修改姓名&lt;&#x2F;button&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">&#x2F;&#x2F; 新增toRefs方法</span><br><span class="line">import &#123;ref, reactive, toRefs&#125; from &#39;vue&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">    setup()&#123;</span><br><span class="line">        let obj &#x3D; reactive(&#123;</span><br><span class="line">            username: &quot;Jack&quot;</span><br><span class="line">        &#125;)</span><br><span class="line">        let btnClick1 &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">            obj.username &#x3D; &quot;Mary&quot;</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; 通过toRefs方法</span><br><span class="line">        let refObj &#x3D; toRefs(obj);</span><br><span class="line">      	&#x2F;&#x2F; 通过...refObj将数据扩展</span><br><span class="line">        return &#123;...clickCountFn(), ...refObj, btnClick1&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 其他代码...</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>


<h2 id="三、Provide与Inject（提供-注入）【掌握】"><a href="#三、Provide与Inject（提供-注入）【掌握】" class="headerlink" title="三、Provide与Inject（提供/注入）【掌握】"></a>三、Provide与Inject（提供/注入）【掌握】</h2><h3 id="1、Vue2写法"><a href="#1、Vue2写法" class="headerlink" title="1、Vue2写法"></a>1、Vue2写法</h3><p>以往我们的父传子是通过props传的：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Father.vue父组件 --&gt;</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;Child :num&#x3D;&quot;num&quot; &#x2F;&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import Child from &#39;.&#x2F;Child.vue&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">    data()&#123;</span><br><span class="line">        return &#123;</span><br><span class="line">            num: 123</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    components: &#123;</span><br><span class="line">        Child</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- Child.vue子组件 --&gt;</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h2&gt;父组件传过来的值：&#123;&#123;num&#125;&#125;&lt;&#x2F;h2&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">    props: [&#39;num&#39;]</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<p>这个时候限制死了数据必须来自父组件，我们其实还有 <code>Provide</code> 和 <code>Inject</code> ：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Father.vue父组件 --&gt;</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;Child &#x2F;&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import Child from &#39;.&#x2F;Child.vue&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">    components: &#123;</span><br><span class="line">        Child</span><br><span class="line">    &#125;,</span><br><span class="line">    provide: &#123;</span><br><span class="line">        num: 456</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- Child.vue子组件 --&gt;</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h2&gt;父组件传过来的值：&#123;&#123;num&#125;&#125;&lt;&#x2F;h2&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">    inject: [&#39;num&#39;]</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<p><code>Provide/Inject</code> 相比于 <code>props</code> 的好处在于：</p>
<p>如果组件嵌套较多，那么 <code>props</code> 需要一级一级往下传递，后期很难维护。<code>Provide+Inject</code> 相当于是跨级组件传值，比如孙子组件也想用上面这个 <code>num</code> 的值，就不用一级一级往下传，直接在孙子组件使用即可：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Sun.vue孙子组件 --&gt;</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h4&gt;孙子组件：&#123;&#123;num&#125;&#125;&lt;&#x2F;h4&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">	  &#x2F;&#x2F; 将Sun组件在Child组件中引入，即可实现跨级组件传值</span><br><span class="line">    inject: [&#39;num&#39;]</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<h3 id="2、Vue3写法"><a href="#2、Vue3写法" class="headerlink" title="2、Vue3写法"></a>2、Vue3写法</h3><p>vue3中的 <code>provide/inject</code>。两者都只能在当前活动实例的 <code>setup()</code> 期间调用。</p>
<p>格式为：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// provide</span></span><br><span class="line"><span class="keyword">import</span> &#123;provide&#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span> <span class="comment">// 显式导入</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="comment">// 此处name必须是String类型，value则不限制</span></span><br><span class="line">    provide(name, value)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// inject</span></span><br><span class="line"><span class="keyword">import</span> &#123;inject&#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span> <span class="comment">// 显式导入</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      	<span class="comment">// name即为传过来的字段，第二个参数可选，可填写默认值</span></span><br><span class="line">        <span class="keyword">const</span> val = inject(name, defaultValue);</span><br><span class="line">      	<span class="keyword">return</span> &#123;val&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们修改以上案例的代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Father.vue父组件 --&gt;</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;Child &#x2F;&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import &#123;provide&#125; from &#39;vue&#39; &#x2F;&#x2F; 显式导入</span><br><span class="line">import Child from &#39;.&#x2F;Child.vue&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">    components: &#123;</span><br><span class="line">        Child</span><br><span class="line">    &#125;,</span><br><span class="line">    setup()&#123;</span><br><span class="line">        provide(&#39;num&#39;, 789)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- Sun.vue孙子组件 --&gt;</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;h4&gt;孙子组件：&#123;&#123;mynum&#125;&#125;&lt;&#x2F;h4&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import &#123;inject&#125; from &#39;vue&#39; &#x2F;&#x2F; 显式导入</span><br><span class="line">export default &#123;</span><br><span class="line">    setup()&#123;</span><br><span class="line">        const mynum &#x3D; inject(&#39;num&#39;);</span><br><span class="line">        return &#123;mynum&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<h3 id="3、响应性"><a href="#3、响应性" class="headerlink" title="3、响应性"></a>3、响应性</h3><p>所谓的 <code>Provide/Inject</code> 响应性，其实就是把传递的值结合上文提及的 <code>ref</code> 或 <code>reactive</code> 一起使用：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Father.vue父组件 --&gt;</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;Child &#x2F;&gt;</span><br><span class="line">  &lt;button @click&#x3D;&quot;changeNumFn&quot;&gt;修改num&lt;&#x2F;button&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import &#123;provide, ref&#125; from &#39;vue&#39; &#x2F;&#x2F; 显式导入</span><br><span class="line">import Child from &#39;.&#x2F;Child.vue&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">    components: &#123;</span><br><span class="line">        Child</span><br><span class="line">    &#125;,</span><br><span class="line">    setup()&#123;</span><br><span class="line">      	&#x2F;&#x2F; 使用ref来定义num的值</span><br><span class="line">        const num &#x3D; ref(123);</span><br><span class="line"></span><br><span class="line">      	&#x2F;&#x2F; 声明一个函数，专门用于修改num</span><br><span class="line">        let changeNumFn &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">            num.value &#x3D; 456;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        provide(&#39;num&#39;, num)</span><br><span class="line"></span><br><span class="line">      	&#x2F;&#x2F; 返回这个函数</span><br><span class="line">        return &#123;changeNumFn&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<p>此时，当你点击按钮时，孙子组件接收到的 <code>num</code> 就会被修改了。</p>
<h2 id="四、Teleport（传送门）【掌握】"><a href="#四、Teleport（传送门）【掌握】" class="headerlink" title="四、Teleport（传送门）【掌握】"></a>四、Teleport（传送门）【掌握】</h2><p>在vue2中，想要将子节点渲染到存在于父组件以外的 <code>DOM</code> 节点时，需要通过第三方库 <a target="_blank" rel="noopener" href="https://links.jianshu.com/go?to=https://github.com/LinusBorg/portal-vue">portal-vue</a> 去实现。而vue3中，<code>Teleport</code> 是一种能够将我们的模板移动到 <code>DOM</code> 中 <code>Vue app</code> 之外的其他位置的技术。</p>
<blockquote>
<p>官方文档：<a target="_blank" rel="noopener" href="https://vue3js.cn/docs/zh/guide/teleport.html">《teleport》</a></p>
</blockquote>
<p>举个最简单的例子：</p>
<p>我们在 <code>index.html</code> 中 <code>#app</code> 同级的地方新增一个 <code>#test</code> 元素：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;test&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>由于vue的 <code>main.js</code> 中规定了打包出来的代码都放入 <code>#app</code> 中：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">createApp(App).mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>因此，你现在没有办法将代码放入 <code>#test</code> 中。此时，我们可以使用传送门：</p>
<p><code>App.vue</code> 中：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;Home &#x2F;&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import Home from &#39;.&#x2F;components&#x2F;Home.vue&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">  name: &#39;App&#39;,</span><br><span class="line">  components: &#123;</span><br><span class="line">    Home</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<p><code>Home.vue</code> 中：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;p&gt;这段话是渲染在#app中的&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;teleport to&#x3D;&quot;#test&quot;&gt;</span><br><span class="line">      &lt;p&gt;这段话是渲染在#test中的--1&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;&#x2F;teleport&gt;</span><br><span class="line">	&lt;teleport to&#x3D;&quot;#test&quot;&gt;</span><br><span class="line">      &lt;p&gt;这段话是渲染在#test中的--2&lt;&#x2F;p&gt;</span><br><span class="line">  &lt;&#x2F;teleport&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br></pre></td></tr></table></figure>
<p>此时，你打开浏览器控制台，就可以看到第2、3个p标签已经被渲染到 <code>#test</code> 中。</p>
<blockquote>
<p>备注：</p>
<p>1、<teleport>标签身上都to属性，填写的是css选择器。</p>
<p>2、多个传送门书写时，会按照自上而下的顺序传送至另一个DOM元素。</p>
</blockquote>
<h2 id="五、Suspense（等待）【掌握】"><a href="#五、Suspense（等待）【掌握】" class="headerlink" title="五、Suspense（等待）【掌握】"></a>五、Suspense（等待）【掌握】</h2><p>Suspense组件用于在等待某个异步组件解析时显示后备内容。</p>
<p>那我们什么时候需要使用异步组件呢？多了去了，比如：</p>
<ul>
<li>在页面加载之前显示加载动画</li>
<li>显示占位符内容</li>
<li>处理延迟加载的图像</li>
</ul>
<p>那么，让我们看看 <code>Suspense</code> 怎么使用，我们先来提一个需求：</p>
<blockquote>
<p>在等待组件获取数据并解析时显示“玩命加载中…”之类的内容</p>
</blockquote>
<p>OK，我们来写一个 <code>Article.vue</code> 组件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;p&gt;&#123;&#123; content &#125;&#125;&lt;&#x2F;p&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import &#123; ref &#125; from &quot;vue&quot;;</span><br><span class="line">export default&#123;</span><br><span class="line">  async setup() &#123;</span><br><span class="line">    let content &#x3D; ref(&#39;内容&#39;)</span><br><span class="line">    content.value &#x3D; await new Promise((resolve,reject)&#x3D;&gt;&#123;</span><br><span class="line">        setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">            &#x2F;&#x2F; 3秒后修改content</span><br><span class="line">            resolve(&quot;你好世界&quot;);</span><br><span class="line">        &#125;, 3000);</span><br><span class="line">    &#125;)</span><br><span class="line">    return &#123;content&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<p>用个 <code>Home.vue</code> 组件来调用它：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;template</span><br><span class="line">  &lt;Suspense&gt;</span><br><span class="line">      &lt;template #default&gt;</span><br><span class="line">          &lt;Article&gt;&lt;&#x2F;Article&gt;</span><br><span class="line">      &lt;&#x2F;template&gt;</span><br><span class="line">      &lt;template #fallback&gt;</span><br><span class="line">          &lt;p&gt;玩命加载中...&lt;&#x2F;p&gt;</span><br><span class="line">      &lt;&#x2F;template&gt;</span><br><span class="line">  &lt;&#x2F;Suspense&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import Article from &#39;.&#x2F;Article.vue&#39;</span><br><span class="line">export default &#123;</span><br><span class="line">    components:&#123;</span><br><span class="line">        Article</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<p>可以看出，<code>&lt;Suspense&gt;</code> 中，包含了两个template标签，<code>#default</code> 定义了我们要写入的内容， <code>#fallback</code> 定义了我们要预显示的内容。</p>
<blockquote>
<p>⚠️ 注意：</p>
<p>作为template中的内容（即插槽），必须要有根元素。</p>
</blockquote>
<h2 id="六、Fragment（碎片）【了解】"><a href="#六、Fragment（碎片）【了解】" class="headerlink" title="六、Fragment（碎片）【了解】"></a>六、Fragment（碎片）【了解】</h2><p>vue2中，如果你创建一个Vue组件，那么它只能有一个根节点。这意味着不能创建这样的组件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">	&lt;div&gt;你好&lt;&#x2F;div&gt;</span><br><span class="line">	&lt;div&gt;世界&lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br></pre></td></tr></table></figure>
<p>原因是代表任何Vue组件的Vue实例需要绑定到一个单一的DOM元素中。唯一可以创建一个具有多个DOM节点的组件的方法就是创建一个没有底层Vue实例的功能组件。</p>
<p>这情况同样存在于react，但react可以使用空标签 <code>&lt;&gt;&lt;/&gt;</code> 来包裹，或者是使用一个名为Fragment的虚拟元素：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Columns</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">    	&lt;React.Fragment&gt;</span><br><span class="line">      	&lt;td&gt;你好&lt;/td&gt;</span><br><span class="line">        &lt;td&gt;世界&lt;/td&gt;</span><br><span class="line">      &lt;/React.Fragment&gt;</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>尽管Fragment看起来像一个普通的DOM元素，但它是虚拟的，根本不会在DOM树中呈现。目前你可以在Vue 2中使用vue-fragments库来使用Fragments，而在Vue 3中，你直接使用就行了，无需引入任何库。</p>
<h2 id="七、TreeShaking（消除未使用代码）【了解】"><a href="#七、TreeShaking（消除未使用代码）【了解】" class="headerlink" title="七、TreeShaking（消除未使用代码）【了解】"></a>七、TreeShaking（消除未使用代码）【了解】</h2><p>TreeShaking 是一个术语，指的是在打包构建过程中移除没有被引用到的代码，这些代码可以成为 <strong>dead code</strong>。这个概念最早在基于 ES6 的打包工具 Rollup 中提出，后来被引入到 webpack 中。TreeShaking 比较依赖于 ES6 模块系统的静态结构特性，比如 <code>import</code> 和 <code>export</code>。</p>
<p>文档参考：<a target="_blank" rel="noopener" href="https://vue3js.cn/docs/zh/guide/migration/global-api-treeshaking.html#_2-x-%E8%AF%AD%E6%B3%95">https://vue3js.cn/docs/zh/guide/migration/global-api-treeshaking.html#_2-x-%E8%AF%AD%E6%B3%95</a></p>
<p>举个例子：</p>
<p>vue2中我们常使用 <code>Vue.nextTick(()=&gt;&#123;&#125;)</code> 来预操作DOM，但有时候我们不用这个 <code>nextTick</code> ，比如改用别的方式来代替（如setTimeout），那么项目打包时，vue 全局的 <code>nextTick</code> 就成为一个多余的代码，从而使你的项目打包体积变大。</p>
<p>在vue3中，官方团队重构了所有全局 API 的组织方式，让所有的 API 都支持了 TreeShaking。所以vue3中如果还想使用全局的 <code>nextTick</code> ，就需要引入：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; nextTick &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;</span><br><span class="line"> </span><br><span class="line">nextTick(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 和 DOM 有关的一些操作</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>如果你在 Vue 3 中不引入而直接调用 <code>Vue.nextTick()</code> ，就会得到一个报错：<code>undefined is not a function</code>。</p>
<p>官方也给出了Vue 2.x 中的受此更改影响的全局 API：</p>
<ul>
<li><code>Vue.nextTick</code></li>
<li><code>Vue.observable</code> (用 <code>Vue.reactive</code> 替换)</li>
<li><code>Vue.version</code></li>
<li><code>Vue.compile</code> (仅全构建)</li>
<li><code>Vue.set</code> (仅兼容构建)</li>
<li><code>Vue.delete</code> (仅兼容构建)</li>
</ul>
<h2 id="八、Performance（性能）【了解】"><a href="#八、Performance（性能）【了解】" class="headerlink" title="八、Performance（性能）【了解】"></a>八、Performance（性能）【了解】</h2><p>vue3.0相对于vue2.0来说性能快1.2到1.5倍，主要原因如下：</p>
<h3 id="1、diff方法优化"><a href="#1、diff方法优化" class="headerlink" title="1、diff方法优化"></a>1、diff方法优化</h3><ul>
<li>Vue2 中的虚拟dom是进行全量的对比</li>
<li>Vue3 新增了静态标记（PatchFlag），只比对带有 PF 的节点，并且通过 Flag 的信息得知 当前节点要比对的具体内容。</li>
</ul>
<h3 id="2、静态提升"><a href="#2、静态提升" class="headerlink" title="2、静态提升"></a>2、静态提升</h3><ul>
<li>Vue2中无论元素是否参与更新, 每次都会重新创建, 然后再渲染</li>
<li>Vue3中对于不参与更新的元素, 会做静态提升, 只会被创建一次, 在渲染时直接复用即可</li>
</ul>
<h3 id="3、cacheHandlers-事件侦听器缓存"><a href="#3、cacheHandlers-事件侦听器缓存" class="headerlink" title="3、cacheHandlers 事件侦听器缓存"></a>3、cacheHandlers 事件侦听器缓存</h3><ul>
<li>默认情况下onClick会被视为动态绑定, 所以每次都会去追踪它的变化</li>
<li>但是因为是同一个函数，所以没有追踪变化, 直接缓存起来复用即可</li>
</ul>
<h3 id="4、ssr渲染"><a href="#4、ssr渲染" class="headerlink" title="4、ssr渲染"></a>4、ssr渲染</h3><ul>
<li>当有大量静态的内容时候，这些内容会被当做纯字符串推进一个buffer里面，  即使存在动态的绑定，会通过模板插值嵌入进去。这样会比通过虚拟dmo来渲染的快上很多很多。</li>
<li>当静态内容大到一定量级时候，会用_createStaticVNode方法在客户端去生成一个static node，  这些静态node，会被直接innerHtml，就不需要创建对象，然后根据对象渲染。</li>
</ul>
<h2 id="九、Setup的生命周期【了解】"><a href="#九、Setup的生命周期【了解】" class="headerlink" title="九、Setup的生命周期【了解】"></a>九、Setup的生命周期【了解】</h2><p>组合式API需要在setup中使用，setup中含有的生命钩子与vue的大体一致：</p>
<p>具体参考：<a target="_blank" rel="noopener" href="https://vue3js.cn/docs/zh/guide/composition-api-lifecycle-hooks.html">《setup生命周期钩子》</a></p>
<h2 id="十、TypeScript支持【了解】"><a href="#十、TypeScript支持【了解】" class="headerlink" title="十、TypeScript支持【了解】"></a>十、TypeScript支持【了解】</h2><p>vue3新增了对TS语法的支持。    </p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://gitee.com/shimmer612/myBlog/2021/02/26/2021-02-26-vue3%E7%AC%94%E8%AE%B0/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myBlog/tags/Vue3/" rel="tag">Vue3</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/myBlog/2021/03/02/2021-03-02-React%E9%A1%B9%E7%9B%AE(UmiJS)%E4%B8%AD%E4%BD%BF%E7%94%A8%E9%AB%98%E5%BE%B7%E5%9C%B0%E5%9B%BE/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            React项目(UmiJS)中使用高德地图
          
        </div>
      </a>
    
    
      <a href="/myBlog/2021/02/26/2021-02-26-%E5%AF%B9Ant-Design-Table%E7%BB%84%E4%BB%B6%E8%BF%9B%E8%A1%8C%E5%B0%81%E8%A3%85/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">对Ant-Design Table组件进行封装</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "xGZKFxfEhEyqsK7YA4NNoQNr-gzGzoHsz",
    app_key: "qCe4kOh0W9rCgNWcv31cO3NF",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "说出你想说的吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <!--
 * @Author: zhangzhen
 * @Date: 2023-02-04 10:30:09
 * @LastEditors: zhangzhen
 * @LastEditTime: 2023-05-23 09:47:12
 * 
-->
<footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2021-2023
        <i class="ri-heart-fill heart_icon"></i> Shimmer
      </li>
    </ul>
    <!-- <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul> -->
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/myBlog/"><img src="/myBlog/images/ayer-side.svg" alt="Shimmer"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/archives">目录</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/photos">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/2021/02/12/2021-02-12-%E5%85%B3%E4%BA%8E%E6%88%91/">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/message">留言板</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/myBlog/js/jquery-2.0.3.min.js"></script>


<script src="/myBlog/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/myBlog/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/myBlog/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/myBlog/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/myBlog/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/myBlog/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>